package com.liulei.controller;

import com.liulei.entities.Customer;
import com.liulei.entities.History;
import com.liulei.entities.Worker;
import com.liulei.service.CustomerService;
import com.liulei.service.HistoryService;
import com.liulei.service.WorkerService;
//import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@RequestMapping("/history")
@Controller
@Slf4j
public class HistoryController {

    @Autowired
    private HistoryService historyService;
    /**
     点击“送水历史管理”，调用HistoryService对象查询所有送水历史信息，然后将信息渲染到前端。最后返回“送水历史”页面
     */
    @RequestMapping("/hisList")
    public String listHistory(Model model) {
        List<History> hisList = historyService.listHistory();
        log.info("history list size = "+hisList.size());
        model.addAttribute("hisList",hisList);
        return "historyList";
    }
    /**
     *
     */
    @RequestMapping("/searchCalcHis")
    public String searchCalcHis(String startDate,String endDate,Model model) {
        List<History> hisList = historyService.calcHistoryByCondition(startDate, endDate);
        log.info("history list size = "+hisList.size());
        model.addAttribute("hisList",hisList);
        model.addAttribute("startDate",startDate);
        model.addAttribute("endDate",endDate);
        return "historyList";
    }

    /**
     送水历史需要用到客户信息，自动装配CustomerService对象
     */
    @Autowired
    private CustomerService customerService;
    /**
     送水历史需要用到送水工信息，自动装配WorkerService对象
     */
    @Autowired
    private WorkerService workerService;

    @RequestMapping("/preSaveHis")
    /**
     点击“添加”按钮执行预修改操作
     步骤：
     1 查询客户列表和送水工列表
     2 将客户列表和送水工列表渲染到前端页面
     3 返回送水历史页面
     */
    public String preSaveHistory(Model model) {
        //查询客户列表和送水工列表，渲染到前端
        List<Customer> custList = customerService.listCustomer();
        List<Worker> workerList = workerService.listWorker();

        model.addAttribute("custList",custList);
        model.addAttribute("workerList",workerList);
        return "historySave";
    }


     /**
     * 在“添加送水历史页面”点击“提交”按钮，将送水历史信息持久化(insert插入)到数据库中，然后返回“送水历史列表”，重新
     * 查询送水历史列表显示新添加的数据
     * 步骤：
     * 1 将客户ID和送水工ID注入到History对象
     * 2 调用HistoryService对象的saveHistory方法将表单采集的数据持久化到数据库
     * 3 重新“查询送水历史信息”，显示新添加的数据
     * @param  workerId 表单采集的送水工编号
     * @param  custId 表单采集的客户编号
     * @param history 表单采集的送水历史信息
     * @return 重定向到送水历史列表路径
     */

    @RequestMapping(value = "/saveHis" ,method = RequestMethod.POST)
    public String saveHistory(Integer workerId,Integer custId,History history) {
        log.info("save History workerId = "+workerId);
        log.info("save History customerId = "+custId);
        log.info("History history = "+history);
        //将送水工编号和客户编号注入daoHistory对象
        Worker worker = new Worker();
        worker.setWid(workerId);

        Customer customer = new Customer();
        customer.setCid(custId);

        history.setCustomer(customer);
        history.setWorker(worker);
        //将History对象持久化到数据库
        int rows = historyService.saveHistory(history);
        log.info("save History rows = "+rows);
        return "redirect:/history/hisList";
    }

    /**
     * 在“送水历史列表”页面，点击“修改”按钮，根据ID查询对应的“送水历史”信息，然后将信息渲染到前端页面
     * 步骤：
     *  1 查询所有客户信息
     *  2 查询所有送水工信息
     *  3 根据ID查询对应的送水历史信息
     *  4 将以上信息渲染到前端页面上
     *  5 跳转到“修改送水历史”页面
     * @param hid  送水历史ID
     * @param model
     * @return “修改送水历史”页面
     */
    @RequestMapping("/preUpdateHis/{hid}")
    public String preUpdateHistory(@PathVariable("hid")Integer hid,Model model) {
        List<Customer> custList = customerService.listCustomer();
        List<Worker> workerList = workerService.listWorker();
        History history = historyService.getHistoryById(hid);
        //将查询的数据渲染到前端页面，完成数据的回显
        model.addAttribute("custList",custList);
        model.addAttribute("workerList",workerList);
        model.addAttribute("history",history);
        //跳转到“修改送水历史”页面
        return "historyUpdate";
    }

    /**
     * 点击“提交”按钮修改送水历史信息，修改完毕重定向执行“送水历史列表”，显示已修改的“送水历史信息”
     * 步骤：
     *  1 将送水工ID和客户ID注入到History对象
     *  2 调用HistoryService对象的updateHistory方法完成修改
     *  3 修改完毕重定向执行“送水历史列表”
     * @param workerId 送水历史对应的送水工ID
     * @param custId   送水历史对应的客户ID
     * @param history 送水历史信息
     * @return 重定向执行“送水历史列表”
     */
    @RequestMapping(value = "/updateHis",method = RequestMethod.POST)
    public String updateHistory(Integer workerId,Integer custId,History history) {
        Worker worker = new Worker();
        worker.setWid(workerId);
        Customer customer = new Customer();
        customer.setCid(custId);
        history.setCustomer(customer);
        history.setWorker(worker);
        //修改送水历史，返回受影响行数
        int rows = historyService.updateHistory(history);
        log.info("update history rows = "+rows);
        return "redirect:/history/hisList";
    }

    /**
     * 处理删除请求
     * 步骤：
     * 1 调用historyDelete对象的deleteHistoryById方法根据送水历史ID删除送水历史信息
     * 2 打印受影响行数
     * 3 返回送水历史列表路径，重新执行查询送水历史列表方法listHistory，显示删除之后的数据
     * @param hid 前端传入的送水历史id hid
     * @return 返回送水历史列表路径，重新执行查询送水历史列表方法listHistory，显示删除之后的数据
     */
    @RequestMapping("/delHistory/{hid}")
    public String deleteHistory(@PathVariable("hid")Integer hid) {
        historyService.deleteHistoryById(hid);
        return "redirect:/history/hisList";
    }

    /**
     * 处理批量请求
     */
    @RequestMapping(value = "/batchDelete",method = RequestMethod.POST)
    @ResponseBody
    public String batchDeleteHistory(String idList) {
        int rows = historyService.batchDeleteHistory(idList);
        if (rows > 0) {
            return "OK";
        }else {
            return "Fail";
        }
    }

}

